################################################################################
# File: controlPanel.py
# Package: itacqos.uddi.views
# Author: Vincent Pretre
# Last update: $LastChangedDate: 2009-02-28 16:25:51 +0000 (Sat, 28 Feb 2009) $
################################################################################

##@package itacqos.uddi.views.controlPanel
#
# This module contains all views linked to the control panel pages

from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404 
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify

from uddi.models import Category, Keyword, WebService, Vendor
from uddi.forms import RegisterForm, UpdateUserForm, VendorForm, ServiceForm

from common import newRender, getUser

import os
import urllib

## Base view (when asking /uddi/controlPanel/)
#
# Calls controlPanel() with "profile" as argument
def controlPanelBase(request):
	return controlPanel(request, "profile")

## This view is just used to factorize user's authentication.
# @param argument Page of the control panel asked by the user
#
# This function just call controlPanelConnection(), controlPanelPublic(), controlPanelReviews() or controlPanelWS() depending on the argument
def controlPanel(request, argument):
	dict = {}
	
	usr = getUser(request)

	if usr['isAuthenticated']:
		dict['isVendor'] = usr['isVendor']
		
		if argument == 'profile':
			return controlPanelProfile(request, dict, usr)
		elif argument == 'connection':
			return controlPanelConnection(request, dict, usr)
		elif argument == 'public':
			return controlPanelPublic(request, dict, usr)
		elif argument == 'reviews':
			return controlPanelReviews(request, dict, usr)
		elif argument == 'webservices':
			return controlPanelWS(request, dict, usr)
		else:
			raise Http404
	else:
		dict['errorMsg'] = 'You must be authenticated to see this page'
		dict['showLoginForm'] = True
		
		return newRender('error.html', dict, request)

## Shows profile of the user
# @param dict A dictionnary containing variables to pass to the template
# @param usr Array obtained with itacqos.uddi.views.common.getUser()
# @return The web page
def controlPanelProfile(request, dict, usr):
	dict['vendor'] = usr['vendor']
	return newRender('controlPanelProfile.html', dict, request)
	
## Update connections informations (login, email and password)
# @param usr Array obtained with itacqos.uddi.views.common.getUser()
# @return The web page
def controlPanelConnection(request, dict, usr):
	user = usr['user']
	
	if request.method == 'POST':
		form = UpdateUserForm(request.POST)
		if form.validate(user):
			user.email = form.cleaned_data['email']
			
			if form.cleaned_data['password'] != '':
				user.set_password(form.cleaned_data['password'])
			
			user.save()
			
			form = UpdateUserForm({'email': user.email})
			dict['message'] = 'Informations updated'
	else:
		form = UpdateUserForm({'email': user.email})
	
	dict['form'] = form

	return newRender('controlPanelConnection.html', dict, request)

	
## Update public profile (only for vendors) - User name, description and website
# @param usr Array obtained with itacqos.uddi.views.common.getUser()
# @return The web page
def controlPanelPublic(request, dict, usr):
	if usr['isVendor']:
		if request.method == 'POST':
			form = VendorForm(request.POST, instance = usr['vendor'])
			if form.is_valid():
				form.save()
				dict['message'] = 'Informations updated'
			
		else:
			form = VendorForm(instance = usr['vendor'])
		
		dict['form'] = form
		return newRender('controlPanelPublic.html', dict, request)
	else:
		dict['errorMsg'] = "Only web services' vendors can see this page"
		dict['showLoginForm'] = True
		
		return newRender('error.html', dict, request)

## Shows reviews
# @param usr Array obtained with itacqos.uddi.views.common.getUser()
# @return The web page
# @todo Add review system.
def controlPanelReviews(request, dict, usr):
	dict['message'] = 'Coming soon'
	return newRender('controlPanelError.html', dict, request)

## Shows list of user's services (only for vendors)
# @param usr Array obtained with itacqos.uddi.views.common.getUser()
# @return The web page
def controlPanelWS(request, dict, usr):
	if usr['isVendor']:
		dict['services'] = usr['vendor'].webservice_set.all()
		template = 'controlPanelWS.html'
	
	else:
		dict['errorMsg'] = "Only web services' vendors can see this page"
		dict['showLoginForm'] = True
		template = 'controlPanelError.html'

	return newRender(template, dict, request)





